<?php
/**
 * Created by PhpStorm OF yuyi.
 * User: YUYI
 * Date: 2018/5/10 0010
 * Time: 下午 2:38
 * Administrator
 */

namespace app\common\model;


use app\common\util\StringUtil;

class Articles extends BaseModel
{
    use StringUtil;
    protected $pk = 'articleId';

    protected $autoWriteTimestamp = 'dateTime';
    protected $createTime = 'createTime';
    protected $updateTime = 'updateTime';

    protected $append = ['statusText'];

    public function user()
    {
        return $this->hasOne('Users','userId','userId')->setEagerlyType(0);
    }

    public function comments()
    {
        return $this->hasMany('Comments','articleId');
    }

    /**
     * 获取文章简介
     * @param $value
     * @param $date
     * @return bool|string
     */
    public function getTabloidAttr($value,$date)
    {
        return substr(strip_tags($date['content']),0,100);
    }

    public function getStatusTextAttr($value,$data)
    {
        switch ($data['status']){
            case 10:return '待审核';
            case 20:return '审核失败';
            case 30:return '待确认';
            case 40:return '未知状态';
            case 99:return '已发布';
        }
    }
}